package com.spring.api.web.controller;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.util.Random;
public class IdentifyingCode {
	/** 
	    * 验证码图片的宽度。 
	    */  
	   private int width = 90;  
	   /** 
	    * 验证码图片的高度。 
	    */  
	   private int height = 36;  
	   /** 
	    * 验证码的数量。 
	    */  
	   private Random random = new Random();  
	     
	   public IdentifyingCode(){}  
	   /** 
	    * 生成随机颜色 
	    * @param fc    前景色 
	    * @param bc    背景色 
	    * @return  Color对象，此Color对象是RGB形式的。 
	    */  
	   public Color getRandomColor(int fc, int bc) {  
	       if (fc > 255)  
	           fc = 200;  
	       if (bc > 255)  
	           bc = 255;  
	       int r = fc + random.nextInt(bc - fc);  
	       int g = fc + random.nextInt(bc - fc);  
	       int b = fc + random.nextInt(bc - fc);  
	       return new Color(r, g, b);  
	   }  
	     
	   /** 
	    * 绘制干扰线 
	    * @param g Graphics2D对象，用来绘制图像 
	    * @param nums  干扰线的条数 
	    */  
	   public void drawRandomLines(Graphics2D g ,int nums ){  
//	       g.setColor(this.getRandomColor(160, 200)) ;  
	       for(int i=0 ; i<nums ; i++){  
//	           int x1 = random.nextInt(width) ;  
//	           int y1 = random.nextInt(height);  
//	           int x2 = random.nextInt(12) ;  
//	           int y2 = random.nextInt(12) ;  
//	           g.drawLine(x1, y1, x2, y2) ; 
			int x = random.nextInt(width); 
			int y = random.nextInt(20);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			if(i==1||i==3||i==5){
				g.setColor(Color.black) ;
			}else{
				g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))) ;  
			}
			if(i<=8)g.drawLine(x, y, xl, yl+y);
			else {
				int xp = random.nextInt(width); 
				int yp = random.nextInt(height);
				g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))) ; 
				Line2D p1=new Line2D.Double(xp,yp,xp,yp);
			    g.draw(p1); 
			}
			
	       }  
	   }  
	     
	   /** 
	    * 获取随机字符串， 
	    *      此函数可以产生由大小写字母，汉字，数字组成的字符串 
	    * @param length    随机字符串的长度 
	    * @return  随机字符串 
	    */  
	   public String drawRandomString(String code,Graphics2D g){  
	       StringBuffer strbuf = new StringBuffer() ;  
	       String temp = "" ;  
	       char[] codes = code.toCharArray();
	       for(int i=0 ; i<codes.length ; i++){  
	    	   temp =  String.valueOf(codes[i]);
	            Color color = new Color(20+random.nextInt(20) , 20+random.nextInt(20) ,20+random.nextInt(20) );  
	            g.setColor(color) ;  
	            //想文字旋转一定的角度  
	            AffineTransform trans = new AffineTransform();  
	            trans.rotate(random.nextInt(45)*3.14/180, 15*i+8, 7) ;  
	            //缩放文字  
	            float scaleSize = random.nextFloat() + 0.8f ;  
	            if(scaleSize>1f)  
	                scaleSize = 1f ;  
	            trans.scale(scaleSize, scaleSize) ;  
	            g.setTransform(trans) ;  
	            g.drawString(temp, 15*i+18, 14) ;  
	              
	            strbuf.append(temp) ;  
	        }  
	        g.dispose() ;  
	        return strbuf.toString() ;  
	    }  
	    public int getWidth() {  
	        return width;  
	    }  
	    public void setWidth(int width) {  
	        this.width = width;  
	    }  
	    public int getHeight() {  
	        return height;  
	    }  
	    public void setHeight(int height) {  
	        this.height = height;  
	    }  
}
